#include "../import/head.h"
#include "os_init.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"

namespace xos_sgsj
{

    os_init::os_init()
    {
        memset( m_szExePath, 0, sizeof( m_szExePath ) );

        m_pAioMgr = 0;
        m_pCommon = 0;
        m_pXos = 0;
        m_pCurlMgr = 0;
        m_pEncryptMgr = 0;
        m_pHttpMgr = 0;
        m_pJsonMgr = 0;
        m_pLog = 0;
        m_pSqliteMgr = 0;
        m_pXmlMgr = 0;
        m_pPackageMgr = 0;

        m_pMiscCallbackThread = 0;
        m_pPackage = 0;
        m_pSerial = 0;
        m_pHeart = 0;
        m_pMisc = 0;
        m_pCrt = 0;
        m_pAio = 0;
    }

    os_init::~os_init()
    {
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int os_init::heart_call()
    {
        int ret = 0;

        if( ( 0 == ret ) && ( !g_pSysData->m_bIsRunning || g_pSysData->m_bQuiting || g_pSysData->m_bQuit ) )
        {
            ret = 1;
        }

        if( 0 == ret )
        {
            data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
            p->init();
            p->m_nMsg = MSG_HEART;
            g_pSysData->m_pImpl->notify( p );
            p = 0;
        }

        return ret;
    }

    int os_init::init()
    {
        int ret = 0;

        if( 0 == ret )
        {
            ret = m_GetCore.init();
        }

        if( 0 == ret )
        {
            m_pXos = m_GetCore.m_pApi;
        }

        if( 0 == ret )
		{
			ret = m_pXos->create( xos::i_xos::XOS_OBJ_CALLBACK, ( void ** )&m_pMiscCallbackThread );
		}

        if( 0 == ret )
		{
			ret = m_pXos->create( xos::i_xos::XOS_OBJ_HEART, ( void ** )&m_pHeart );
		}

        if( 0 == ret )
        {
            ret = m_pXos->create( xos::i_xos::XOS_OBJ_MISC, ( void ** )&m_pMisc );
		}

        if( 0 == ret )
        {
            ret = m_pXos->create( xos::i_xos::XOS_OBJ_CRT, ( void ** )&m_pCrt );
        }

		if( 0 == ret )
		{
			m_pMisc->get_process_path( m_szExePath, sizeof( m_szExePath ), 0 );
		}

		if( 0 == ret )
        {
            ret = m_GetCommon.init();
        }

        if( 0 == ret )
        {
            ret = m_GetXml.init();
        }

        if( 0 == ret )
        {
            ret = m_GetLog.init();
        }

        if( 0 == ret )
        {
            ret = m_GetAio.init();
        }

        if( 0 == ret )
        {
            ret = m_GetCurl.init();
        }

        if( 0 == ret )
        {
            ret = m_GetEncrypt.init();
        }

        if( 0 == ret )
        {
            ret = m_GetPackage.init();
        }

		if( 0 == ret )
        {
            ret = m_GetHttp.init();
        }

		if( 0 == ret )
        {
            ret = m_GetJson.init();
        }

		if( 0 == ret )
        {
            ret = m_GetSqlite.init();
        }

        return ret;
    }

    int os_init::term()
    {
        int ret = 0;

        if( m_pMiscCallbackThread )
		{
			m_pMiscCallbackThread->release();
			m_pMiscCallbackThread = 0;
		}

        if( m_pHeart )
		{
			m_pHeart->release();
			m_pHeart = 0;
		}

        m_GetSqlite.term();
        m_GetJson.term();
        m_GetHttp.term();
        m_GetPackage.term();
        m_GetEncrypt.term();
        m_GetCurl.term();
		m_GetAio.term();
        m_GetLog.term();
        m_GetXml.term();
		m_GetCommon.term();

        if( m_pMisc )
        {
            m_pMisc->release();
            m_pMisc = 0;
        }

        if( m_pCrt )
        {
            m_pCrt->release();
            m_pCrt = 0;
        }

        m_GetCore.term();

        m_pAioMgr = 0;
        m_pCommon = 0;
        m_pXos = 0;
        m_pCurlMgr = 0;
        m_pPackageMgr = 0;
        m_pEncryptMgr = 0;
        m_pHttpMgr = 0;
        m_pJsonMgr = 0;
        m_pLog = 0;
        m_pSqliteMgr = 0;
        m_pXmlMgr = 0;
        m_pMiscCallbackThread = 0;
        m_pPackage = 0;
        m_pSerial = 0;
        m_pHeart = 0;
        m_pMisc = 0;
        m_pCrt = 0;
        m_pAio = 0;

        return ret;
    }

} // xos_sgsj
